package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * @author Q
 * @create 2025-07-12-15:46
 */
public class LoginPage extends Utils {
    private static String url = "http://43.138.236.60:8080/login.html";
    public LoginPage() {
        super(url);
    }
    /**
     * 测试页面是否正确加载
     */
    public void loginPageRight() {
        //测试网页聊天是否正常加载
        driver.findElement(By.cssSelector("body > div.nav"));
        //测试登录框是否正确加载
        driver.findElement(By.cssSelector("body > div.login-container > div"));
    }
    /**
     * 测试是否可以正常登录
     */
    public void LoginSuc() throws InterruptedException {
        //1.清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
//        driver.navigate().refresh();//刷新清除文本框
        //2.输入账号
        driver.findElement(By.cssSelector("#username")).sendKeys("张三");
        //3.输入密码
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        //4.点击登录
        driver.findElement(By.cssSelector("#submit")).click();
        //5.判断是否登录成功(通过左上角是否有用户名)
        //会出现警告弹窗，处理弹窗
        //1.显示等待,并切换到弹窗页面
        Alert alert = new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.alertIsPresent());
        //2.点击确认
        alert.accept();
        driver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user"));
        //为了测试异常登录，将页面回退到登录页面
        driver.navigate().back();
    }
    /**
     * 测试异常登录
     *      清空输入框
     *          1.clear()
     *          2.刷新页面
     */
    public void loginFail() {
        //1.清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
//        driver.navigate().refresh();//刷新清除文本框
        //2.输入错误登录信息，并登录
        driver.findElement(By.cssSelector("#username")).sendKeys("张三");
        driver.findElement(By.cssSelector("#password")).sendKeys("12345");
        driver.findElement(By.cssSelector("#submit")).click();
        //3.判断是否检测出错误登录，并打印出错弹窗内容
        Alert alert = new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.alertIsPresent());
        String text = alert.getText();
        System.out.println(text);
        //4.点击确定
        alert.accept();
    }
}
